/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.service;

import com.mycompany.delivery.dao.AccountDAO;
import com.mycompany.delivery.dto.AccountDTO;
import com.mycompany.delivery.entities.Account;
import com.mycompany.delivery.servicelayer.AccountCRUDService;
import com.mycompany.delivery.transfers.AccountT;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author mhunek
 */
@Service(value = "accountCRUDService")
@Transactional
public class AccountCRUDServiceImpl implements AccountCRUDService {

    @Autowired
    private AccountDAO accountDAO;

    public AccountDAO getAccountDAO() {
        return accountDAO;
    }

    public void setAccountDAO(AccountDAO accountDAO) {
        this.accountDAO = accountDAO;
    }

    @Transactional
    public void initItData() {
     /*    Account acc1 = new Account();
         acc1.setEnabled(true);
         acc1.setLogin("admin");
         acc1.setPassword("admin");
                  
         HashSet<String> roles = new HashSet<>();
         roles.add("ROLE_ADMIN");
         roles.add("ROLE_USER");
         acc1.setRoles(roles);
         accountDAO.create(acc1);

           Account acc2 = new Account();
         acc1.setEnabled(true);
         acc1.setLogin("user");
         acc1.setPassword("user");
                  
         HashSet<String> roles2 = new HashSet<>();
                  
         roles2.add("ROLE_USER");
                 
         accountDAO.create(acc2);*/
    }

    @Override
    @Transactional
    public AccountDTO createAccount(AccountDTO account) {

        if (account == null) {
            throw new NullPointerException("account is null");
        }
        if (Objects.equals(account.getId(), null) || Objects.equals(account.getId(), 0l)) {
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            account.setPassword(passwordEncoder.encode(account.getPassword()));
            Account acc = AccountT.AccountFromDTO(account);
            accountDAO.create(acc);
            account = AccountT.AccountDTOFromEntity(acc);
           
            return account;

        } else {
            throw new IllegalArgumentException("id need to be null or zero");
        }

    }

    @Override
    @Transactional
    @Secured({"ROLE_ADMIN"})
    public AccountDTO updateAccount(AccountDTO account) {
        if (account == null) {
            throw new IllegalArgumentException("account is null");
        }
        if (account.getId() == null) {
            throw new IllegalArgumentException("id can't be null");
        }
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        account.setPassword(passwordEncoder.encode(account.getPassword()));
        accountDAO.updateAccount(AccountT.AccountFromDTO(account));
        return account;
    }

    @Override
    @Transactional
    @Secured({"ROLE_ADMIN"})
    public void deleteAccount(AccountDTO account) {
        if (account == null) {
            throw new IllegalArgumentException("account is null");
        }
        if (account.getId() == null) {
            throw new IllegalArgumentException("id can't be null");
        }
        accountDAO.deleteAccount(AccountT.AccountFromDTO(account));
    }

    @Override
    @Transactional
    public AccountDTO findAccount(String login) {
        if (login == null) {
            throw new IllegalArgumentException("account is null");
        }
        Account acc = accountDAO.getByLogin(login);
        if (acc == null) {
            return null;
        }
        return AccountT.AccountDTOFromEntity(acc);
    }

    @Override
    @Transactional
    public AccountDTO findAccount(Long id) {
        if (id == null || id == 0l) {
            throw new IllegalArgumentException("account is null");
        }
        Account acc = accountDAO.getAccountById(id);
        if (acc == null) {
            return null;
        }
        return AccountT.AccountDTOFromEntity(acc);
    }

    @Override
    @Transactional
    public List<AccountDTO> findAllAccount() {
        List<Account> accounts = accountDAO.getAllContacts();
        List<AccountDTO> accountsDTO = new ArrayList<>();
        for (Account acc : accounts) {
            accountsDTO.add(AccountT.AccountDTOFromEntity(acc));
        }
        return accountsDTO;
    }
}
